package com.ruoyi.file.utils;

import com.ruoyi.common.core.utils.DateUtils;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URL;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

public class WatermarkUtils {

    public static File addTextWatermark(MultipartFile file, String descriptionText, String addressText, String gpsText) throws IOException {
        BufferedImage originalImage = ImageIO.read(file.getInputStream());
        int width = originalImage.getWidth();
        int height = originalImage.getHeight();

        // 获取当前时间和日期
        LocalDateTime now = LocalDateTime.now();
        String timeText = now.format(DateTimeFormatter.ofPattern("HH:mm"));
        String dateText = now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        // 格式化星期
        String weekText = now.format(DateTimeFormatter.ofPattern("EEEE", Locale.CHINA));

        // 创建新图片用于绘制水印，保持图像类型与原始图像一致
        BufferedImage watermarkedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2d = watermarkedImage.createGraphics();
        g2d.drawImage(originalImage, 0, 0, null); // 先绘制原图

        // 设置抗锯齿和插值
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);

        // 1. 绘制时间
        Font timeFont = new Font("微软雅黑", Font.BOLD, 80);  // 放大字体，使其占两行高度
        g2d.setFont(timeFont);

        // 获取时间字体的高度
        FontMetrics timeFontMetrics = g2d.getFontMetrics();

        int timeTextX = 50;
        int timeTextY = height - 150; // 向上移动 150 像素
        g2d.setColor(new Color(0, 0, 0, 128)); // 投影颜色（浅色黑色），透明度为50%
        g2d.drawString(timeText, timeTextX + 3, timeTextY + 3 - 10); // 绘制投影
        g2d.setColor(new Color(255, 255, 255, 255)); // 白色，不透明
        g2d.drawString(timeText, timeTextX, timeTextY - 10); // 绘制时间文本 向上提10个点

        // 2. 计算分割图标大小和位置
        BufferedImage segmentationIcon = readImageFromResources("segmentation.png");
        int segmentationIconHeight = 70; // 固定高度
        int segmentationIconWidth = 5; // 固定宽度
        Image scaledSegmentationIcon = segmentationIcon.getScaledInstance(segmentationIconWidth, segmentationIconHeight, Image.SCALE_SMOOTH);

        int segmentationIconX = timeTextX + timeFontMetrics.stringWidth(timeText) + 10;  // 图标位置，时间右侧偏移 10px
        int segmentationIconY = timeTextY - 65; // 向上移动 65 像素
        g2d.drawImage(scaledSegmentationIcon, segmentationIconX, segmentationIconY - 10, null);

        // 3. 设置日期和星期的字体并绘制它们
        // 日期字体大小为 28 号
        Font dateFont = new Font("微软雅黑", Font.BOLD, 28);
        g2d.setFont(dateFont);
        FontMetrics dateFontMetrics = g2d.getFontMetrics();

        int dateTextX = segmentationIconX + segmentationIconWidth + 10;  // 图标右侧偏移 10px
        int dateTextY = segmentationIconY + 26; // 与分割图标顶部对齐+26 往下移动
        g2d.setColor(new Color(0, 0, 0, 128)); // 投影颜色（浅色黑色），透明度为50%
        g2d.drawString(dateText, dateTextX + 3, dateTextY + 3 - 10); // 绘制投影
        g2d.setColor(new Color(255, 255, 255, 255)); // 白色，不透明
        g2d.drawString(dateText, dateTextX, dateTextY - 10);

        // 星期字体大小为 22 号
        Font weekFont = new Font("微软雅黑", Font.BOLD, 22);
        g2d.setFont(weekFont);
        FontMetrics weekFontMetrics = g2d.getFontMetrics();

        int weekTextX = dateTextX; // 与日期对齐
        int weekTextY = dateTextY + dateFontMetrics.getHeight();
        g2d.setColor(new Color(0, 0, 0, 128)); // 投影颜色（浅色黑色），透明度为50%
        g2d.drawString(weekText, weekTextX + 3, weekTextY + 3 - 10); // 绘制投影
        g2d.setColor(new Color(255, 255, 255, 255)); // 白色，不透明
        g2d.drawString(weekText, weekTextX, weekTextY - 10);

        g2d.setFont(new Font("微软雅黑", Font.BOLD, 20));

        // 4. 绘制描述文本
        int descriptionTextX = timeTextX + 10; // 与时间对齐
        int descriptionTextY = weekTextY + weekFontMetrics.getHeight() + 10; // 向下移动
        g2d.setColor(new Color(0, 0, 0, 128)); // 投影颜色（浅色黑色），透明度为50%
        g2d.drawString(descriptionText, descriptionTextX + 3, descriptionTextY + 3); // 绘制投影
        g2d.setColor(new Color(255, 255, 255, 255)); // 白色，不透明
        g2d.drawString(descriptionText, descriptionTextX, descriptionTextY);

        // 5. 绘制地址文本
        int addressTextX = descriptionTextX; // 与描述文本对齐
        int addressTextY = descriptionTextY + weekFontMetrics.getHeight() + 4; // 向下移动
        g2d.setColor(new Color(0, 0, 0, 128)); // 投影颜色（浅色黑色），透明度为50%
        g2d.drawString(addressText, addressTextX + 3, addressTextY + 3); // 绘制投影
        g2d.setColor(new Color(255, 255, 255, 255)); // 白色，不透明
        g2d.drawString(addressText, addressTextX, addressTextY);

        // 6. 绘制经纬度文本
        int gpsTextX = timeTextX + 10; // 靠左对齐
        int gpsTextY = addressTextY + weekFontMetrics.getHeight() + 4; // 向下移动
        g2d.setColor(new Color(0, 0, 0, 128)); // 投影颜色（浅色黑色），透明度为50%

//       取消经纬度
        g2d.drawString(gpsText, gpsTextX + 3, gpsTextY + 3); // 绘制投影
        g2d.setColor(new Color(255, 255, 255, 255)); // 白色，不透明
        g2d.drawString(gpsText, gpsTextX, gpsTextY);

        // 释放资源
        g2d.dispose();

        // 创建临时文件
        File watermarkFile = File.createTempFile("watermark_", ".jpg");
        ImageIO.write(watermarkedImage, "jpg", watermarkFile);
        return watermarkFile;
    }

    private static BufferedImage readImageFromResources(String fileName) throws IOException {
        ClassLoader classLoader = WatermarkUtils.class.getClassLoader();
        URL resource = classLoader.getResource(fileName);
        if (resource == null) {
            throw new IOException("资源文件未找到：" + fileName);
        }
        return ImageIO.read(resource);
    }

    public static void main(String[] args) {
        try {
            // 读取本地文件并创建MultipartFile对象
            File file = new File("C:\\Users\\24971\\Documents\\xwechat_files\\yuxi103020_cc75\\msg\\file\\2025-01\\上传资料\\1.png"); // 替换为实际图片路径
            FileInputStream input = new FileInputStream(file);
            MultipartFile multipartFile = new MockMultipartFile("file", file.getName(), "image/jpeg", input);

            // 添加水印
            String descriptionText = "① 装车称重照片";
            String addressText = "重庆市渝北区金开大道98号再生资源有限公司";
            String gpsText = "纬度：40.71283  经度：74.00606";
            File watermarkedFile = addTextWatermark(multipartFile, descriptionText, addressText, gpsText);

            // 保存水印后的文件
            File outputFile = new File("C:\\Users\\24971\\Documents\\xwechat_files\\yuxi103020_cc75\\msg\\file\\2025-01\\上传资料\\watermarked_image.jpg"); // 替换为实际输出路径
            ImageIO.write(ImageIO.read(watermarkedFile), "jpg", outputFile);

            System.out.println("水印添加成功，文件保存到: " + outputFile.getAbsolutePath());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}